package com.zkzx.wlhy.adaptor.service.driver;

import com.alibaba.fastjson.JSON;
import com.zkzx.common.constants.NationalityEnum;
import com.zkzx.common.constants.SourceEnum;
import com.zkzx.common.constants.YesNOEnum;
import com.zkzx.common.driver.command.DriverCommand;
import com.zkzx.common.driver.constants.DriverAlidityEnum;
import com.zkzx.common.driver.constants.DriverSourceEnum;
import com.zkzx.common.driver.constants.DriverStatusEnum;
import com.zkzx.common.driver.model.DriverBankVO;
import com.zkzx.common.driver.model.DriverLicenseVO;
import com.zkzx.common.driver.model.DriverQualificationVO;
import com.zkzx.common.driver.model.DriverVO;
import com.zkzx.common.driver.param.DriverInvitationParam;
import com.zkzx.common.driver.param.DriverThreeElementsParam;
import com.zkzx.common.driver.vo.DriverAlidityVO;
import com.zkzx.common.exception.BaseException;
import com.zkzx.common.exception.ExceptionEnum;
import com.zkzx.common.global.Global;
import com.zkzx.util.date.DateUtil;
import com.zkzx.wlhy.adaptor.clinet.ResultVO;
import com.zkzx.wlhy.adaptor.clinet.bestSign.BestSignClient;
import com.zkzx.wlhy.adaptor.clinet.driver.DriverClient;
import com.zkzx.wlhy.adaptor.controller.driver.param.DriverDTO;
import com.zkzx.wlhy.adaptor.controller.driver.vo.*;
import com.zkzx.wlhy.adaptor.service.code.WlhyCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/20/13:29
 * @Description:
 */
@Slf4j
@Service
public class DriverService {


     @Autowired
     private DriverClient driverClient;
     @Autowired
     private BestSignClient bestSignClient;


    /**
     * 查询是否存在该司机
     * @param  id  司机id
     * @param name  司机姓名
     *  @param idCard  司机身份证号
     */
    public DriverCommand getDriver(Integer id, String name, String  idCard){
        ResultVO resultVO = driverClient.getDriver(name, idCard);

        DriverCommand driverCommand = new DriverCommand();
        driverCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.NO_SUBMIT.name());
            driverCommand.setSource(SourceEnum.ONESELF.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FOUR.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.VERIFY_SUCCESS.name());
            WlhyDriverVO wlhyDriverVO = JSON.parseObject(JSON.toJSONString(resultVO.getData()),  WlhyDriverVO.class);
            driverCommand = assembleDriver(id, driverCommand, wlhyDriverVO);
        }else {
            throw new BaseException(ExceptionEnum.WH_DATA_ERROR.getCode(), ExceptionEnum.WH_DATA_ERROR.getMsg());
        }

        return driverCommand;
    }


    /**
     *上传司机信息
     * @param  id  司机id
     * @param captcha 手机验证码
     *  @param identityKey  司机手机号三要素验证码
     */
    public DriverCommand add(Integer id, DriverVO driverVO,String  captcha,String  identityKey) {
        DriverDTO driverDTO = new DriverDTO();
        driverDTO.setCompanyId(driverVO.getCompanyThreeId());
        driverDTO.setAddPlatfor(Global.ONE);
        driverDTO.setBankAccoun(driverVO.getDriverBank().getCardNumber());
        driverDTO.setBankName(driverVO.getDriverBank().getName());
        driverDTO.setBankPhone(driverVO.getDriverBank().getMobile());
        driverDTO.setProvince(driverVO.getProvince());
        driverDTO.setCity(driverVO.getCity());
        driverDTO.setCountry(driverVO.getCounty());
        driverDTO.setAddress(driverVO.getIdentificationAddress());
        driverDTO.setDriverLicense(driverVO.getDriverLicense().getFrontPic());
        driverDTO.setDriverLicenseBack(driverVO.getDriverLicense().getSubpagePic());
        driverDTO.setIdCardValidityPeriodBegin(DateUtil.convertStringDateToStringDateTime(driverVO.getIdentificationStartTime()));
        //身份证长期
        driverDTO.setIdCardValidityPeriod(DriverAlidityEnum.LONG.name().equals(driverVO.getDriverAlidityVO().getValue())
                ? Global.TWO_THOUSAND_AND_NINETY_NINE_YYYYMMDD_HHMMSS
                : DateUtil.convertStringDateToStringDateTime(driverVO.getIdentificationEndTime()));
        //驾驶证长期，非必填
        if (DriverAlidityEnum.LONG.name().equals(driverVO.getDriverLicense().getDriverAlidityVO().getValue())){
            driverDTO.setDriverLicenseValidityPeriod(Global.TWO_THOUSAND_AND_NINETY_NINE_YYYYMMDD_HHMMSS);
        }
        driverDTO.setIdCard(driverVO.getIdentification());
        driverDTO.setIdCardBack(driverVO.getIdentificationBackPic());
        driverDTO.setIdCardFront(driverVO.getIdentificationFrontPic());
        driverDTO.setIssuingOrganizations(driverVO.getDriverLicense().getAuthority());
        driverDTO.setName(driverVO.getName());
        driverDTO.setNationCode(NationalityEnum.valueOf(driverVO.getDriverNationVO().getValue()).getCode());
        driverDTO.setPersonalIdentity3Key(identityKey);
        driverDTO.setQualication(driverVO.getDriverQualification().getPic());
        driverDTO.setQualicationId(driverVO.getDriverQualification().getCode());
        driverDTO.setQualicationValidityPeriod(DateUtil.convertStringDateToStringDateTime(driverVO.getDriverQualification().getEndTime()));
        driverDTO.setTelephone(driverVO.getMobile());
        driverDTO.setType(Global.ONE.toString());
        driverDTO.setValidPeriodFrom(DateUtil.convertStringDateToStringDateTime(driverVO.getDriverLicense().getStartTime()));
        driverDTO.setValidPeriodPo(DateUtil.convertStringDateToStringDateTime(driverVO.getDriverLicense().getEndTime()));
        driverDTO.setVcode(captcha);
        driverDTO.setVehicleClass(driverVO.getDriverLicense().getType());

        ResultVO resultVO = driverClient.add(driverDTO);

        DriverCommand driverCommand = new DriverCommand();
        driverCommand.setId(id);
        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
           driverCommand.setThreeId(String.valueOf(resultVO.getData()));
           driverCommand.setStatus(DriverStatusEnum.APPROVE_ING.name());
           driverCommand.setSource(DriverSourceEnum.ONESELF.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FOUR.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.VERIFY_SUCCESS.name());
            driverCommand.setSource(SourceEnum.SHARE.name());
            //添加数据在数据库已存在且状态为审核通过

            WlhyDriverVO wlhyDriverVO = JSON.parseObject(JSON.toJSONString(resultVO.getData()),  WlhyDriverVO.class);

            driverCommand = assembleDriver(id, driverCommand, wlhyDriverVO);
        }else if (WlhyCode.FOUR_HUNDRED_AND_THREE.equals(resultVO.getCode())){
            //三要素失败403
            driverCommand.setStatus(DriverStatusEnum.ACTIVATED_FAIL.name());
            driverCommand.setRemarks(resultVO.getMessage());

        }else if (WlhyCode.TWO_HUNDRED_AND_FIVE.equals(resultVO.getCode()) || WlhyCode.TWO_HUNDRED_AND_ONE.equals(resultVO.getCode())){
            //205、401、网货有，恢复成未提交
            driverCommand.setStatus(DriverStatusEnum.NO_SUBMIT.name());
            driverCommand.setRemarks(resultVO.getMessage());
        }else{
            // 400添加错误信息、和其他，都认为审核驳回
            driverCommand.setStatus(DriverStatusEnum.APPROVE_FAIL.name());
            driverCommand.setRemarks(resultVO.getMessage());

        }

        return driverCommand;
    }

    /**
     *查询添加司机审核状态
     * @param id  司机id
     * @param threeId 三方司机id
     */
    public DriverCommand getStatus(Integer id,String threeId){
        ResultVO<String> resultVO = driverClient.getStatus(threeId);

        DriverCommand driverCommand = new DriverCommand();
        driverCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.CONTRACT_FAIL.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FIVE.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.APPROVE_ING.name());
        }else if (WlhyCode.FOUR_HUNDRED_AND_ONE.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.APPROVE_FAIL.name());
        }else if (WlhyCode.FOUR_HUNDRED.equals(resultVO.getCode())){
            //查询参数错误
            driverCommand.setStatus(DriverStatusEnum.APPROVE_FAIL.name());
            driverCommand.setRemarks(ExceptionEnum.DRIVER_WH_DESCRIPTION_ERROR.getMsg());
        }else {
            //403或其他
            driverCommand.setStatus(DriverStatusEnum.APPROVE_FAIL.name());
            driverCommand.setRemarks(resultVO.getMessage());
        }

        return driverCommand;
    }


    /**
     * 司机手机号三要素验证码获取
     * @param id  司机id
     * @param driverThreeElementsParam 机号三要素信息
     */
    public DriverCommand getIdentityKey(Integer id, DriverThreeElementsParam driverThreeElementsParam) {

        ResultVO<WlhyDriverThreeElementsVO> resultVO = bestSignClient.getIdentityKey(driverThreeElementsParam);

        DriverCommand driverCommand = new DriverCommand();
        driverCommand.setId(id);

        //key用remark
        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode()) || WlhyCode.FOUR_HUNDRED.equals(resultVO.getCode())){
            if (Global.ONE.toString().equals(resultVO.getData().getResult())){
               driverCommand.setRemarks(resultVO.getData().getPersonalIdentity3Key());
               driverCommand.setStatus(DriverStatusEnum.ACTIVATED_WAIT.name());
            }else if (Global.ZERO.toString().equals(resultVO.getData().getResult())){
                driverCommand.setStatus(DriverStatusEnum.ACTIVATED_FAIL.name());
                driverCommand.setRemarks(resultVO.getData().getMsg());
            }
        }else if (WlhyCode.FOUR_HUNDRED_AND_THREE.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.ACTIVATED_FAIL.name());
            driverCommand.setRemarks(ExceptionEnum.DRIVER_WH_IDCARD_ERROR.getMsg());
        }else {
            driverCommand.setStatus(DriverStatusEnum.ACTIVATED_FAIL.name());
            driverCommand.setRemarks(resultVO.getMessage());
        }

        return driverCommand;
    }

    /**
     *邀请司机签约
     * @param id  司机id
     * @param id  driverInvitationParam 司机签约信息
     */
    public DriverCommand getContractSign(Integer id, DriverInvitationParam driverInvitationParam) {

        ResultVO<WlhyDriverInvitationVO> resultVO = bestSignClient.getContractSign(driverInvitationParam);

        DriverCommand driverCommand = new DriverCommand();
        driverCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){

                DriverQualificationVO driverQualificationVO = new DriverQualificationVO();
                driverQualificationVO.setDriverId(id);
                driverQualificationVO.setContractNum(resultVO.getData().getContractNum());
                driverQualificationVO.setContractStatus(YesNOEnum.NO.name());
                driverCommand.setDriverQualification(driverQualificationVO);
                driverCommand.setStatus(DriverStatusEnum.CONTRACT_ING.name());

        }else if (WlhyCode.FOUR_HUNDRED.equals(resultVO.getCode())){
            driverCommand.setStatus(DriverStatusEnum.CONTRACT_FAIL.name());
            driverCommand.setRemarks(resultVO.getData().getErrmsg());
        }
        return driverCommand;
    }

    /**
     *查询合同签署情况
     * @param id  司机id
     * @param contractId 合同编号
     *  @param
     */
    public DriverCommand getContractStatus(Integer id,String contractId,String  mobile){
        ResultVO resultVO = bestSignClient.getContractStatus(contractId);

        DriverCommand driverCommand = new DriverCommand();
        driverCommand.setId(id);
        DriverQualificationVO driverQualificationVO = new DriverQualificationVO();
        driverQualificationVO.setDriverId(id);
        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode()) ){
            WlhyDriverContractStatusVO wlhyDriverContractStatusVO = JSON.parseObject(JSON.toJSONString(resultVO.getData()), WlhyDriverContractStatusVO.class);
            Map<String, String> data = wlhyDriverContractStatusVO.getData();
            String statusCode = data.get(mobile);
            if (Global.ONE.toString().equals(statusCode)){
                driverCommand.setStatus(DriverStatusEnum.CONTRACT_ING.name());
                driverQualificationVO.setContractStatus(YesNOEnum.NO.name());
            }else if (Global.TWO.toString().equals(statusCode)){
                driverCommand.setStatus(DriverStatusEnum.VERIFY_SUCCESS.name());
                driverQualificationVO.setContractStatus(YesNOEnum.YES.name());
            }

        }else {
            //403和其他状态，默认失败
                driverCommand.setStatus(DriverStatusEnum.CONTRACT_FAIL.name());
                driverCommand.setRemarks(resultVO.getMessage());

        }
        driverCommand.setDriverQualification(driverQualificationVO);
        return driverCommand;
    }




    public DriverCommand assembleDriver(Integer id,DriverCommand driverCommand,WlhyDriverVO wlhyDriverVO){
        driverCommand.setId(id);
        driverCommand.setThreeId(String.valueOf(wlhyDriverVO.getId()));
        driverCommand.setIdentificationFrontPic(wlhyDriverVO.getIdCardFront());
        driverCommand.setIdentificationBackPic(wlhyDriverVO.getIdCardBack());
        driverCommand.setIdentificationAddress(wlhyDriverVO.getAddress());
        driverCommand.setIdentificationStartTime(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getIdCardValidityPeriodBegin()));

        //身份证长期
        if (Global.TWO_THOUSAND_AND_NINETY_NINE_YYYYMMDD_HHMMSS.equals(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getIdCardValidityPeriod()))){
            driverCommand.setIdentificationStatus(DriverAlidityEnum.LONG.name());
        }else {
            driverCommand.setIdentificationStatus(DriverAlidityEnum.SHORT.name());
            driverCommand.setIdentificationEndTime(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getIdCardValidityPeriod()));
        }
        driverCommand.setNation(NationalityEnum.getNameByCode(wlhyDriverVO.getNationCode()));
        driverCommand.setDriverPic(wlhyDriverVO.getAvatar());
        driverCommand.setProvince(wlhyDriverVO.getProvince());
        driverCommand.setCity(wlhyDriverVO.getCity());
        driverCommand.setCounty(wlhyDriverVO.getCountry());
        DriverBankVO driverBankVO = new DriverBankVO();
        driverBankVO.setDriverId(id);
        driverBankVO.setCardNumber(wlhyDriverVO.getBankAccount());
        driverBankVO.setMobile(wlhyDriverVO.getBankPhone());
        driverBankVO.setName(wlhyDriverVO.getBankName());

        DriverLicenseVO driverLicenseVO = new DriverLicenseVO();
        driverLicenseVO.setDriverId(id);
        driverLicenseVO.setAuthority(wlhyDriverVO.getIssuingOrganizations());
        driverLicenseVO.setFrontPic(wlhyDriverVO.getDriverLicense());
        driverLicenseVO.setStartTime(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getValidPeriodFrom()));
        driverLicenseVO.setType(wlhyDriverVO.getVehicleClass());
        //驾驶证长期
        if (Global.TWO_THOUSAND_AND_NINETY_NINE_YYYYMMDD_HHMMSS.equals(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getDriverLicenseValidityPeriod()))){
            driverLicenseVO.setDriverAlidityVO(DriverAlidityVO.builder()
                    .value(DriverAlidityEnum.LONG.name())
                    .build());
        }else {
            driverLicenseVO.setEndTime(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getValidPeriodPo()));
            driverLicenseVO.setDriverAlidityVO(DriverAlidityVO.builder()
                    .value(DriverAlidityEnum.SHORT.name())
                    .build());
        }
        DriverQualificationVO driverQualificationVO = new DriverQualificationVO();
        driverQualificationVO.setDriverId(id);
        driverQualificationVO.setCode(wlhyDriverVO.getQualicationId());
        driverQualificationVO.setPic(wlhyDriverVO.getQualication());
        driverQualificationVO.setEndTime(DateUtil.convertStringDateTimeToStringDate(wlhyDriverVO.getQualicationValidityPeriod()));

        driverCommand.setDriverBank(driverBankVO);
        driverCommand.setDriverLicense(driverLicenseVO);
        driverCommand.setDriverQualification(driverQualificationVO);

        return driverCommand;
    }

}

